#include "logger.h"

#include <stdexcept>

using namespace std;


//------------------------------------------------------------------------------
// Public Functions

Logger::Logger()
{

}

Logger::~Logger()
{
    close();
}

void Logger::open(string p_file_name)
{
    ofstream *file;
    
    file = new ofstream();
    file->open(p_file_name.c_str(), ios::trunc | ios::out);
    
    // Add it to the map file.
    m_files[p_file_name] = file;
}

void Logger::close()
{
    map<string, ofstream *>::iterator it;

    for(it = m_files.begin(); it != m_files.end(); ++it) {
        (*it).second->close();
        delete (*it).second;
    }

    m_files.clear();
}

ofstream &Logger::log(std::string p_file_name)
{
    if(m_files.find(p_file_name) != m_files.end()) {
        // The file exists and is open.
        return *(m_files[p_file_name]);
    }

    throw out_of_range("Specified file doesn't exist.");
}


//------------------------------------------------------------------------------
// Private Functions
